package com.gmail.blackdog1987.carcare;

import in.srain.cube.Cube;
import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.SDKInitializer;
//import com.baidu.mapapi.SDKInitializer;
import com.gmail.blackdog1987.carcare.activity.LoginActivity;
import com.gmail.blackdog1987.carcare.db.DbHelper;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TUser;
import com.gmail.blackdog1987.carcare.util.LogUtil;
import com.gmail.blackdog1987.carcare.util.NetworkUtils;

/**
 * 
 * @author 董国超
 * 
 */
public class App extends Application {

	/** Application实例 */
	private static App sInstance;
	/** 网络助手实例 */
	private static NetworkUtils sNetworkUtils;
	/** 数据库助手实例 */
	private static DbHelper sDbHelper;
	
	private static HashMap<String, String> userNameCache = new HashMap<String, String>();
	/** 油价 */
	public static Map<String, String> GAS_PRICE_MAP = new HashMap<String, String>();
	/** 充值金额 */
	public static Map<String, List<String>> CHARGE_VALUE_MAP = new HashMap<String, List<String>>(); 
	/** 模块动态配置 */
	public static Map<String, Integer> FUNCTION_CONFIG = new HashMap<String, Integer>();
	/** 模块动态配置 */
	public static Map<String, Integer> PRICE_CONFIG = new HashMap<String, Integer>();
	
	public static BDLocation location;

	@Override
	protected void finalize() throws Throwable {
		if (sDbHelper != null && sDbHelper.isOpen()) {
			sDbHelper.close();
		}
		sDbHelper = null;
		sInstance = null;
		super.finalize();
	}

	/**
	 * 获取数据库助手实例。
	 * 
	 * @return 数据库助手实例。
	 */
	public static DbHelper getDbHelper() {
		if (sDbHelper == null) {
			sDbHelper = DbHelper.getDefault(getInstance());
			sDbHelper.open();
		}
		return sDbHelper;
	}

	@Override
	public void onCreate() {
		sInstance = this;
		super.onCreate();
		
		// init baidu map datas
		SDKInitializer.initialize(this);

		// init avos congif

	    try {
	      Class<?> avosclass = Class.forName("com.avos.avoscloud.AVOSCloud");
	      Method enableLogMethod = avosclass.getDeclaredMethod("showInternalDebugLog", boolean.class);
	      enableLogMethod.setAccessible(true);
	      enableLogMethod.invoke(avosclass, true);
	      com.avos.avoscloud.LogUtil.avlog.i("successed enable avoscloud logs");
	    } catch (Exception e) {
	    	com.avos.avoscloud.LogUtil.avlog.i("failed enable avoscloud logs");
	    }
		
		LogUtil.setLogLevel(Log.ASSERT);
		// LogUtil.setLogLevel(Log.ASSERT); // 设置LogCat输出等级
		LogUtil.setLogLevel(Log.DEBUG); // 设置LogCat输出等级
		getDbHelper();
		Cube.onCreate(this);
	}
	
	@Override
    public void onTerminate() {
        super.onTerminate();
        Cube.onTerminate();
    }

	/**
	 * 获取Application实例。
	 * 
	 * @return Application实例。
	 */
	public static App getInstance() {
		return sInstance;
	}

	/**
	 * 获取网络助手实例。
	 * 
	 * @return 网络助手实例。
	 */
	public static NetworkUtils getNetworkHelper() {
		if (sNetworkUtils == null) {
			sNetworkUtils = new NetworkUtils();
		}
		return sNetworkUtils;
	}

	public static String lookupname(String peerId) {
		return userNameCache.get(peerId);
	}

	public static void registerLocalNameCache(String peerId, String name) {
		userNameCache.put(peerId, name);
	}
	
	public static boolean validUser(final Activity act){
		TUser user = new UserDao().getUser();
		if(user==null){
			new AlertDialog.Builder(act).setTitle("温馨提示").setMessage("您还没登录，现在登录？").setPositiveButton("登录", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					act.startActivity(new Intent(act, LoginActivity.class));
					dialog.dismiss();
				}
			}).setNegativeButton("不用了", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).create().show();
			return false;
		}else{
			return true;
		}
	}
	
	public static void loadImg(CubeImageView img, ImageLoader imageLoader, String url){
		if(url!=null&&url.trim().length()!=0){
			if(!url.startsWith("http")){
				url = Constant.IMG_PREFIX + url;
			}
			img.loadImage(imageLoader, url);
		}
	}
}
